home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 March / CMCD0305.ISO / Software / Shareware / Utilitare / emu / Emu8086_Setup_307c.exe / {app} / Samples / robot.asm < prev    next >
Assembly Source File  |  2002-08-02  |  3KB  |  166 lines

  1. #MAKE_BIN#
  2. #CS = 500#
  3. #DS = 500#
  4. #SS = 500#    ; stack is set
  5. #SP = FFFF#   ; automatically.
  6. #IP = 0#
  7.  
  8. ; This is an example of controlling
  9. ; the robot attached to a computer.
  10.  
  11. ; This code randomly moves the robot
  12. ; and makes it to switch off/on the
  13. ; lamps.
  14.  
  15. ; Try improving it!
  16.  
  17. ; Keep in mind that robot is a
  18. ; mechanical creature and it takes
  19. ; some time for it to complete
  20. ; a task.
  21.  
  22. ; robot base I/O port:
  23. R_PORT EQU 9
  24.  
  25. ;===================================
  26.  
  27. eternal_loop:
  28. ; wait until robot
  29. ; is ready:
  30. CALL WAIT_ROBOT
  31.  
  32. ; examine the area
  33. ; in front of the robot:
  34. MOV AL, 4
  35. OUT R_PORT, AL
  36.  
  37. CALL WAIT_EXAM
  38.  
  39. ; get result from
  40. ; data register:
  41. IN AL, R_PORT + 1
  42.  
  43. ; nothing found?
  44. CMP AL, 0
  45. JE cont  ; - yes, so continue.
  46.  
  47. ; wall?
  48. CMP AL, 255  
  49. JE cont  ; - yes, so continue.
  50.  
  51. ; switched-on lamp?
  52. CMP AL, 7
  53. JNE lamp_off  ; - no, so skip.
  54. ; - yes, so switch it off,
  55. ;   and turn:
  56. CALL SWITCH_OFF_LAMP 
  57. JMP  cont  ; continue
  58.  
  59. lamp_off: NOP
  60.  
  61. ; if gets here, then we have
  62. ; switched-off lamp, because
  63. ; all other situations checked
  64. ; already:
  65. CALL SWITCH_ON_LAMP
  66.  
  67. cont:
  68. CALL RANDOM_TURN
  69.  
  70. CALL WAIT_ROBOT
  71.  
  72. ; try to step forward:
  73. MOV AL, 1
  74. OUT R_PORT, AL
  75.  
  76. CALL WAIT_ROBOT
  77.  
  78. ; try to step forward again:
  79. MOV AL, 1
  80. OUT R_PORT, AL
  81.  
  82. JMP eternal_loop ; go again!
  83.  
  84. ;===================================
  85.  
  86. ; This procedure does not
  87. ; return until robot is ready
  88. ; to receive next command:
  89. WAIT_ROBOT PROC
  90. ; check if robot busy:
  91. busy: IN AL, R_PORT+2
  92.       TEST AL, 00000010b
  93.       JNZ busy ; busy, so wait.
  94. RET    
  95. WAIT_ROBOT ENDP
  96.  
  97. ;===================================
  98.  
  99. ; This procedure does not
  100. ; return until robot completes
  101. ; the examination:
  102. WAIT_EXAM PROC
  103. ; check if has new data:
  104. busy2: IN AL, R_PORT+2
  105.        TEST AL, 00000001b
  106.        JZ busy2 ; no new data, so wait.
  107. RET    
  108. WAIT_EXAM ENDP
  109.  
  110. ;===================================
  111.  
  112. ; Switch Off the lamp:
  113. SWITCH_OFF_LAMP PROC
  114. MOV AL, 6
  115. OUT R_PORT, AL
  116. RET
  117. SWITCH_OFF_LAMP ENDP
  118.  
  119. ;===================================
  120.  
  121. ; Switch On the lamp:
  122. SWITCH_ON_LAMP PROC
  123. MOV AL, 5
  124. OUT R_PORT, AL
  125. RET
  126. SWITCH_ON_LAMP ENDP
  127.  
  128. ;===================================
  129.  
  130. ; Generates a random turn using
  131. ; system timer:
  132. RANDOM_TURN PROC
  133.  
  134. ; get number of clock
  135. ; ticks since midnight
  136. ; in CX:DX
  137. MOV AH, 0
  138. INT 1Ah
  139.  
  140. ; randomize using XOR:
  141. XOR DH, DL
  142. XOR CH, CL
  143. XOR CH, DH
  144.  
  145. TEST CH, 2
  146. JZ no_turn
  147.  
  148. TEST CH, 1
  149. JNZ turn_right
  150.  
  151. ; turn left:
  152. MOV AL, 2
  153. OUT R_PORT, AL
  154. ; exit from procedure:
  155. RET  
  156.  
  157. turn_right:
  158. MOV AL, 3
  159. OUT R_PORT, AL
  160.  
  161. no_turn:
  162. RET
  163. RANDOM_TURN ENDP
  164.  
  165. ;===================================
  166.